<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: lxf
  Date: 2016/12/14
  Time: 14:19
  To change this template use File | Settings | File Templates.
--%>
<html>
<head>
    <title>QRCode</title>
    <script src="/static/jquery-2.1.0.min.js"></script>
    <script src="/static/qrcode/jquery.qrcode.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var str = "http://www.baidu.com";
            $('#code').qrcode(str);

            $("#sub_btn").click(function () {
                $("#code").empty();
                var str = toUtf8($("#mytxt").val());
                $("#code").qrcode({
                    render: "canvas",
                    /*render: "table", canvas效率高，需支持h5 */
                    width: 200,
                    height: 200,
                    text: str
                });
            });
        });
        function toUtf8(str) {
            var out, i, len, c;
            out = "";
            len = str.length;
            for (i = 0; i < len; i++) {
                c = str.charCodeAt(i);
                if ((c >= 0x0001) && (c <= 0x007F)) {
                    out += str.charAt(i);
                } else if (c > 0x07FF) {
                    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                    out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                } else {
                    out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                }
            }
            return out;
        }
    </script>
</head>
<body>
<div id="main">
    <h2 class="top_title">二维码生成</h2>
    <div class="demo">
        <p>
            <textarea class="input" id="mytxt" cols="50" rows="5"></textarea>
            <input type="button" id="sub_btn" class="btn btn-primary" value="生成">
        </p>
        <div id="code"></div>
    </div>
</div>
</body>
</html>
